/*
 * Copyright 2016 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */
package edu.study.common.util.eventbus;

import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;

/**
 * 类EventListener.java的实现描述：
 * 
 * @author zhangjinbao 2016年1月25日 下午3:48:38
 */
@Slf4j
public class EventListener {

	/**
	 * 监听
	 * @param event
	 * @throws Exception
	 */
    @Subscribe
    @AllowConcurrentEvents
    public void listen(Event event) throws Exception {
        if (null == event) {
            log.warn("EventListener.listen() event is null, do nothing");
            return;
        }
        //目标方法参数列表
        Object[] args = event.getArgs();
        //目标方法参数类型列表
        @SuppressWarnings("rawtypes")
        Class[] parameterTypes = event.getClazzs();
        if (null == parameterTypes) {
            if (null != args && args.length > 0) {
                parameterTypes = new Class[args.length];
                for (int i = 0; i < args.length; i++) {
                    parameterTypes[i] = args[i].getClass();
                }
            }
        }
        //取bean的方法对象
        Method method = event.getBean().getClass().getDeclaredMethod(event.getMethodName(), parameterTypes);
        method.setAccessible(true);
        //调用目标方法
        method.invoke(event.getBean(), args);
    }
}
